<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="产品名称">
        <el-input v-model="queryParams.name" placeholder="产品名称" />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="请选择">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="queryParams.type" placeholder="请选择">
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="top-fun">
      <el-button type="success" @click="handleAdd">新增</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="	产品名称	 " align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="	产品类型  " align="center">
        <template slot-scope="scope">
          {{ scope.row.type=='limit_number'?'限购份数':'限购金额' }}
        </template>
      </el-table-column>
      <el-table-column label="产品id	" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="	排序  " align="center">
        <template slot-scope="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="利息" align="center">
        <template slot-scope="scope">
          {{ (scope.row.interest*100).toFixed(3) }}
        </template>
      </el-table-column> -->
      
      <el-table-column label="	是否新手产品 " align="center">
        <template slot-scope="scope">
          {{ scope.row.isRookiePro }}
        </template>
      </el-table-column>
      <el-table-column label="	定期时长	 " align="center">
        <template slot-scope="scope">
          {{ scope.row.lockDays }}
        </template>
      </el-table-column>

      <el-table-column label="	状态  " align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.statusB"
            @change="setStatus(scope.row)"
          />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            class="copy-btn"
            @click="handleshowInfo(scope.row)"
          >查看</el-button>
          <el-button
            type="text"
            class="copy-btn"
            @click="handleEdit(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        :current-page.sync="queryParams.pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size.sync="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="fetchData"
        @current-change="fetchData"
      />
    </div>
    <Add ref="add" />
    <ShowInfo ref="ShowInfo" />
  </div>
</template>

<script>
import { productList, productUserEnable } from '@/api/table'
import Add from './add.vue'
import ShowInfo from './showInfo.vue'
export default {
  components: {
    Add,
    ShowInfo
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      date: [],
      statusOptions: [
        { label: '下架', value: '0' },
        { label: '上架', value: '1' }
      ],
      typeOptions: [
        { label: '限购份数', value: 'limit_number' },
        { label: '限购金额', value: 'limit_money' }
      ],
      queryParams: {
        'name': '',
        'pageNum': 1,
        'pageSize': 10,
        'status': '',
        'type': ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  mounted() {

  },
  methods: {
    async setStatus(user) {
      let status = ''
      if (user.status === 1) {
        status = 0
      } else {
        status = 1
      }
      await productUserEnable({ id: user.id, status: status })
      this.fetchData()
      this.$message({
        message: '操作成功',
        type: 'success'
      })
    },
    fetchData() {
      this.listLoading = true
      productList(this.queryParams).then(response => {
        response.data.records.map(item => {
          if (item.status === 1) {
            item.statusB = true
          } else {
            item.statusB = false
          }
          console.log(item.statusB)
        })
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },

    onSubmit() {
      this.queryParams.pageNum = 1

      this.fetchData()
    },
    handleAdd() {
      this.$refs.add.show()
    },
    handleEdit(e) {
      this.$refs.add.edit(e)
    },
    handleshowInfo(e) {
      this.$refs.ShowInfo.show(e)
    },
    reset() {
      this.value1 = ''
      this.value2 = ''
      this.queryParams = {
        'name': '',
        'pageNum': 1,
        'pageSize': 10,
        'status': '',
        'type': ''
      }
      this.fetchData()
    }

  }
}
</script>
